package com.briup.ch11;

import java.io.*;

/**
 * @Author briup-adam
 * @Date 2023/10/20 上午8:07
 * @Description 使用缓冲流完成文件拷贝
 * @see java.io.BufferedReader
 * @see java.io.BufferedWriter
 *
 **/

public class CopyFileByBuffered {
   public static void copy(String  src,String dest){
       //声明缓冲字符流
      FileReader fr=null;
      FileWriter fw=null;
      BufferedReader br=null;
      BufferedWriter bw=null;
      try {
         fr=new FileReader(src);
         fw=new FileWriter(dest);
         br=new BufferedReader(fr);
         bw=new BufferedWriter(fw);

         String line = null;
         while((line=br.readLine())!=null){
            bw.write(line);
            //当即将读取到文件末尾的时候会返回false
            if (br.ready())
               bw.newLine();
         }
         bw.flush();
      } catch (Exception e) {
         e.printStackTrace();
      }finally {
         try {
            if (br!=null)
               br.close();
            if (bw!=null)
               bw.close();
            if (fr!=null)
               fr.close();
            if (fw!=null)
               fw.close();

         }catch (Exception e){
            e.printStackTrace();
         }
      }

   }

   public  static  void  copy2(String src,String  dest){
      FileReader fr=null;
      BufferedReader br=null;
      PrintWriter  pw=null;

      try {
         fr=new FileReader(src);
         br=new BufferedReader(fr);
         pw=new PrintWriter(dest);
         String  line=null;
         while ((line=br.readLine())!=null){
            if (br.ready())
               pw.println(line);
            else
               pw.print(line);
         }
      } catch (Exception e) {
         e.printStackTrace();
      }finally {
         try {
            if (pw!=null)
               pw.close();
            if (br!=null)
               br.close();
            if (fr!=null)
               fr.close();

         }catch (Exception e){

         }
      }

   }
   public static void main(String[] args) {
      copy("02code/src/背影Dest.txt","a.txt");
//      copy2("02code/src/橘子大使.txt","a.txt");
   }
}
